<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceNumber">
        <el-input
          v-model="queryParams.deviceNumber"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备状态" prop="deviceStatus">
        <el-select v-model="queryParams.deviceStatus" placeholder="请选择设备状态" clearable>
          <el-option
            v-for="dict in deviceStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备id" align="center" prop="deviceId" />
      <el-table-column label="设备编号" align="center" prop="deviceNumber" />
      <el-table-column label="设备地址" align="center" prop="deviceAddress" />
      <el-table-column label="设备状态" align="center" prop="deviceStatus">
        <template slot-scope="scope">
          {{typeFormatone(scope.row)}}
        </template>
      </el-table-column>
      <el-table-column label="铃声音量" align="center" prop="ringVolume" />
      <el-table-column label="报警音量" align="center" prop="policeVolume" />
      <el-table-column label="设备通知" align="center" :formatter="typeFormat" prop="deviceNotice"/>
      <el-table-column label="设备音频" align="center" prop="deviceAudio" >
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-headset" @click="play">播放</el-button>
          <audio controls id="myAudio" v-show="false" style="width: 100px">
            <source :src="Url(scope.row.deviceAudio)">
          </audio>
        </template>
      </el-table-column>
      <el-table-column label="设备降噪" align="center" prop="deviceDenoise" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-tools"
            @click="handleSetUp(scope.row)"
            v-hasPermi="['business:device:edit']"
          >设备设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:device:remove']"
          >删除</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="police('故障')"-->
<!--            v-hasPermi="['business:device:remove']"-->
<!--          >报警器播放故障</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="police('报警')"-->
<!--            v-hasPermi="['business:device:remove']"-->
<!--          >报警器播放报警</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceNumber">
          <el-input v-model="form.deviceNumber" disabled placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备地址" prop="deviceAddress">
          <el-input v-model="form.deviceAddress" placeholder="请输入设备地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel('addAndEdit')">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 设备配置对话框 -->
    <el-dialog title="设备配置" :visible.sync="setUp.open" width="1000px" append-to-body>
      <div class="number">设备编号：{{this.setUp.form.deviceNumber}}</div>
      <el-tabs style ="font-size: 40px" v-model="activeName">
        <el-tab-pane label="用户设备管理" name="first">
<!--          <set ref="set" :deviceId="deviceId"/>-->
          <set ref="set" :deviceId="deviceId"/>
        </el-tab-pane>
        <el-tab-pane label="敏感词配置" name="second">
<!--          <model/>-->
          <model/>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setUps">确 定</el-button>
        <el-button @click="cancel('setUp')">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { listDevice, getDevice, delDevice, addDevice, updateDevice,police } from "@/api/patrol/device";


import model from "./model.vue"
import set from "./set.vue"

export default {
  name: "Device",
  components:{model,set},
  data() {
    return {
      //设置对象
      setUp:{
        form:{},
        open:false
      },
      selec:[],
      //Tabs 标签页
      activeName: 'first',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      //地址数据
      options:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNumber: null,
        deviceStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceNumber: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        deviceAddress: [
          { required: true, message: "设备地址不能为空", trigger: "blur" }
        ],
        deviceStatus: [
          { required: true, message: "设备状态不能为空", trigger: "change" }
        ],
      },
      deviceId:null,
      deviceStatus:{}
    };
  },
  created() {
    this.getList();
    this.getDicts("device_status").then(response => {
      this.deviceStatus = response.data;
    });
    this.getDicts("notice_type").then(response => {
      this.noticeType = response.data;
    });
  },
  methods: {
    police(val){
      police(val)
    },
    play(){
      document.getElementById("myAudio").play()
    },
    // 报警类型字典翻译
    typeFormat(row) {
      console.log(row)
      return this.selectDictLabel(this.noticeType, row.deviceNotice);
    },
    typeFormatone(row, column) {
      return this.selectDictLabel(this.deviceStatus, row.deviceStatus);
    },
    /** 给路径加上工程地址 */
    Url(src){
      return process.env.VUE_APP_BASE_API+src
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel(val) {
      switch (val){
        case "addAndEdit":this.open = false;break;
        case "setUp":this.setUp.open = false;break;
      }
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        personnelId: null,
        deviceNumber: null,
        deviceAddress: null,
        deviceStatus: null,
        deviceVolume: null,
        deviceNotice: null,
        deviceAudio: null,
        deviceDenoise: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 设置设备产数 */
    handleSetUp(row) {
      this.reset();
      this.deviceId = row.deviceId || this.ids
      this.setUp.open = true
      this.$nextTick(() => {
        if (this.$refs.set) {
          this.$refs.set.getDevice();
        } else {
          console.error('set 组件的引用不存在');
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    setUps(){
      this.$modal.msgSuccess("修改配置成功");
      this.setUp.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + deviceIds + '"的数据项？').then(function() {
        return delDevice(deviceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

